home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0387.arc / DAWSON.ARC / SIMTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-07-12  |  12.1 KB  |  408 lines

  1. /*******************************************\
  2. *                         *
  3. *   simtest.c = Test program for SIMPP:        *
  4. *   Simple IMage Processing Package.        *
  5. *   Copyright (c) 1987, Benjamin M. Dawson  *
  6. *      Edit Version: 1.1 : Jan-29-87        *
  7. *                         *
  8. \*******************************************/
  9.  
  10. #include <stdio.h>
  11. #include "simpp.h"
  12.  
  13. /* These error flags are returned by the exit() routine.  I have used the
  14.  * values appropriate for UNIX.  These values may not give the desired
  15.  * results under VMS, RT-11, etc., so you may have to change them to
  16.  * values appropriate for your system.
  17.  */
  18. #define ERROR_EXIT    1        /* Signal ERROR exit */
  19. #define OK_EXIT        0        /* Signal no error exit */
  20.  
  21. /***** Kernels *****/
  22. static int kersh1[]    = {-1, -1, -1,        /* Kernel for sharpening */
  23.               -1,  9, -1,
  24.               -1, -1, -1,};
  25.  
  26. static int kersh2[] =    {-1, -1, -1,        /* Kernel for sharpening */
  27.               -1, 10, -1,        /* Not as strong as #1 */
  28.               -1, -1, -1,};        /* Use scale factor of 1 */
  29.  
  30. static int kersh3[] =    {-1, -1, -1,        /* Kernel for sharpening */
  31.               -1, 12, -1,        /* Not as strong as #1 or #2 */
  32.               -1, -1, -1,};        /* Use a scale of 2 */
  33.  
  34. static int kerhoriz[] = { -1, -1, -1, -1, -1,    /* Kernel for horiz. edges */
  35.                0,  0,  0,  0,  0,
  36.                1,  1,  1,  1,  1,};
  37.  
  38. static int kervert[] = { -1, 0, 1,        /* Kernel for vertical edges */
  39.              -1, 0, 1,
  40.              -1, 0, 1,
  41.              -1, 0, 1,
  42.              -1, 0, 1,};
  43.  
  44. static int kerlapla[] = {-1, -1, -1,        /* Kernel for laplacian */
  45.              -1,  8, -1,
  46.              -1, -1, -1,};
  47.  
  48. static int kerblur[] = { 1, 1, 1, 1, 1, 1, 1, 1,
  49.              1, 1, 1, 1, 1, 1, 1, 1,
  50.              1, 1, 1, 1, 1, 1, 1, 1,
  51.              1, 1, 1, 1, 1, 1, 1, 1,
  52.              1, 1, 1, 1, 1, 1, 1, 1,
  53.              1, 1, 1, 1, 1, 1, 1, 1,
  54.              1, 1, 1, 1, 1, 1, 1, 1,
  55.              1, 1, 1, 1, 1, 1, 1, 1,
  56.             };
  57.  
  58. static long h[PIXEL_SIZE] = 0L;            /* Histogram array */
  59. static char name[80] = 0;            /* File name */
  60.  
  61. #define MAX_QUAD 5    /* Maximum quadrant number */
  62.  
  63. /* Define "quadrants" of image memory, plus "quadrant 0" for the
  64.    entire image memory.  Used as a shorthand for image areas */
  65. #define QUAD0 XSTART,YSTART,XSIZE,YSIZE
  66. #define QUAD1 XSIZE/2,YSTART,XSIZE/2,YSIZE/2
  67. #define QUAD2 XSTART,YSTART,XSIZE/2,YSIZE/2
  68. #define QUAD3 XSTART,YSIZE/2,XSIZE/2,YSIZE/2
  69. #define QUAD4 XSIZE/2,YSIZE/2,XSIZE/2,YSIZE/2
  70. #define QUAD5 XSIZE/4,YSIZE/4,XSIZE/2,YSIZE/2
  71.  
  72. static int qmap[MAX_QUAD+1][4] ={        /* Quadrant array map */
  73.              { QUAD0 },        /* Entire screen */
  74.              { QUAD1 },        /* Upper right */
  75.              { QUAD2 },        /* Upper left */
  76.              { QUAD3 },        /* Lower left */
  77.              { QUAD4 },        /* Lower right */
  78.              { QUAD5 },        /* Center */
  79.                };
  80. /* Define macro to use quad map */
  81. #define QUADMAC(i) qmap[i][0],qmap[i][1],qmap[i][2],qmap[i][3]
  82.  
  83. /* ================================================================ */
  84.  
  85. main()
  86. {
  87.     register int i;            /* General variable */
  88.     int x,y,z;            /* Location and value variables */
  89.     int out;            /* Output quadrant */
  90.     char s[20];            /* Input command string */
  91.     char sharp[5];            /* Sharpen command string */
  92.     float xs,ys;            /* Stretch factors */
  93.     float gbk,gbl,gbm;        /* Gaussian burn factors */
  94.  
  95. /* Salutations! */
  96.     printf("****** SIMPP test program V1.10 ******\n");
  97.     printf("Copyright (c) 1987, Benjamin M. Dawson\n\n");
  98.  
  99. /* Open hardware */
  100.     printf("-- Open hardware --\n");
  101.     if (sim_open() == ERROR) {
  102.         printf("<simtest> Can't open hardware!\n");
  103.         exit(ERROR_EXIT);
  104.     }
  105.  
  106. /* Linearize LUTS, if you have them */
  107.     lin_luts();
  108.     printf("\n");
  109.  
  110.     for (;;) {
  111.  
  112. /* Print menu */
  113.       printf("Acquire\t    Binarize    Brighten    Burn\n");
  114.       printf("Blur\t    Clear area\tCopy area    Contrast Stretch\n");
  115.       printf("Grid\t    Histogram    Horizontal Edges\n");
  116.       printf("Label        Laplacian    Linear LUTs\n");
  117.       printf("Negate\t    Plot Histogram    Print Histogram\n");
  118.       printf("Quit        Rotate        Restore image\n");
  119.       printf("Save image    Sharpen        Spectrum\n");
  120.       printf("Stretch\t    Test all    Vertical Edges\n");
  121.       printf("Command>>> ");
  122.  
  123.       scanf("%s",s);    /* Get command string, with appended NULL */
  124.  
  125. /* Match command */      
  126.       if (matches(s,"acquire",4)) acquire();
  127.       else if (matches(s,"binarize",4)) {
  128.         i = get_quad();
  129.         binarize(QUADMAC(i),get_int("Threshold ="));
  130.       }
  131.       else if (matches(s,"brighten",4)) {
  132.         i = get_quad();
  133.         brighten(QUADMAC(i),get_int("Value to add="));
  134.       }
  135.       else if (matches(s,"blur",4)) {
  136.         i = get_quad();
  137.         convolve(QUADMAC(i),8,8,kerblur,6,POSITIVE);
  138.       }
  139.       else if (matches(s,"burn",4)) {
  140.         i = get_quad();
  141.         printf("Burn factor (real) =");
  142.         scanf("%f",&gbk);
  143.         printf("Space constant (real) =");
  144.         scanf("%f",&gbl);
  145.         printf("Offset (real) =");
  146.         scanf("%f",&gbm);
  147.         gauss_burn(QUADMAC(i),(double)gbk,(double)gbl,(double)gbm);
  148.       }
  149.       else if (matches(s,"clear",4)) {
  150.         i = get_quad();
  151.         clear_area(QUADMAC(i),(PIXEL)get_int("Clear to intensity ="));
  152.       }
  153.       else if (matches(s,"copy",4)) {
  154.         printf("Copy ");
  155.         i = get_quad();
  156.         printf("To ");
  157.         out = get_quad();
  158.         copy_area(QUADMAC(i),QUADMAC(out));
  159.       }
  160.       else if (matches(s,"contrast",4)) {
  161.         i = get_quad();
  162.         cstretch(QUADMAC(i),get_int("Minimum bin count ="));
  163.       }
  164.       else if (matches(s,"grid",4)) {
  165.         x = get_int("X spacing =");
  166.         y = get_int("Y spacing =");
  167.         z = get_int("Intensity =");
  168.         draw_grid(x,y,(PIXEL)z);
  169.       }
  170.       else if (matches(s,"histogram",4)) {
  171.         i = get_quad();
  172.         histogram(QUADMAC(i),h);
  173.       }
  174.       else if (matches(s,"horizontal",4)) {
  175.         i = get_quad();
  176.         convolve(QUADMAC(i),5,3,kerhoriz,0,ABSOLUTE);
  177.       }
  178.       else if (matches(s,"label",4)) {
  179.         i = get_quad();
  180.         x = get_int("Minimum area size =");
  181.         label(QUADMAC(i),MINPIX,MAXPIX,x,
  182.             (PIXEL)1,(PIXEL)(PIXEL_SIZE-2));
  183.       }
  184.       else if (matches(s,"laplacian",4)) {
  185.         i = get_quad();
  186.         convolve(QUADMAC(i),3,3,kerlapla,0,ABSOLUTE);
  187.       }
  188.       else if (matches(s,"linear",4)) lin_luts();
  189.       else if (matches(s,"negate",4)) {
  190.         i = get_quad();
  191.         negate(QUADMAC(i));
  192.       }
  193.       else if (matches(s,"plot",4)) {
  194.         i = get_quad();
  195.         plot_histo(QUADMAC(i),h,
  196.             (PIXEL)get_int("Intensity to plot with = "));
  197.       }
  198.       else if (matches(s,"print",4)) print_histogram(h);
  199.       else if (matches(s,"quit",4)) {
  200.         sim_close();
  201.         exit(OK_EXIT);
  202.       }
  203.       else if (matches(s,"restore",4)) {
  204.         i = get_quad();
  205.         printf("File name:");
  206.         scanf("%s",name);
  207.         read_image(QUADMAC(i),name);
  208.       }
  209.       else if (matches(s,"rotate",4)) {
  210.         i = get_quad();
  211.         rotate(QUADMAC(i));
  212.       }
  213.       else if (matches(s,"save",4)) {
  214.         i = get_quad();
  215.         printf("File name:");
  216.         scanf("%s",name);
  217.         save_image(QUADMAC(i),name);
  218.       }
  219.       else if (matches(s,"sharpen",4)) {
  220.         i = get_quad();
  221.         printf("Degree of sharpening: High, Medium, or Low:");
  222.         scanf("%s",sharp);
  223.         if (matches(sharp,"high",3)) 
  224.             convolve(QUADMAC(i),3,3,kersh1,0,POSITIVE);
  225.         else if (matches(sharp,"medium",3)) 
  226.             convolve(QUADMAC(i),3,3,kersh2,1,POSITIVE);
  227.         else if (matches(sharp,"low",3)) 
  228.             convolve(QUADMAC(i),3,3,kersh3,2,POSITIVE);
  229.       }
  230.       else if (matches(s,"spectrum",4)) p_spectrum();
  231.       else if (matches(s,"stretch",4)) {
  232.         i = get_quad();
  233.         printf("X stretch factor (real) = ");
  234.         scanf("%f",&xs);
  235.         printf("Y stretch factor (real) = ");
  236.         scanf("%f",&ys);
  237.         stretch(QUADMAC(i),(double)xs,(double)ys);
  238.       }
  239.       else if (matches(s,"test",4)) test_all();
  240.       else if (matches(s,"vertical",4)) {
  241.         i = get_quad();
  242.         convolve(QUADMAC(i),3,5,kervert,0,ABSOLUTE);
  243.       }
  244.  
  245.       else printf("?? Not a valid command!\n");
  246.       }
  247. }
  248.  
  249. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  250. static VOID test_all()        /* Script to test the package */
  251. {
  252.     register int i;
  253.     char c;
  254.  
  255. /* Point operations */
  256.     printf("\n Test Point operations:\n");
  257.     printf("-- Take a picture --\n"); acquire();
  258.     printf("-- Negate second Quadrant --\n"); negate(QUAD2);
  259.     printf("-- Brighten first Quadrant by 30 --\n"); brighten(QUAD1,30);
  260.     printf("-- Contrast stretch third Quadrant --\n"); cstretch(QUAD3,30);
  261.  
  262. #ifdef LUTS
  263.     printf("\nTest Output LUTs:\n");
  264.     p_spectrum();        /* Set luts to spectrum */
  265.     printf("Type return (ENTER) to continue:");
  266.     scanf("%c",&c);
  267.     lin_luts();        /* Linearize LUTS */
  268. #endif
  269.  
  270. /* Area operations */
  271.     printf("\nTest Area operations:\n");
  272.  
  273.     printf("-- Take a picture --\n"); acquire();
  274.     printf("-- Sharpen image in Quadrant 1 --\n");
  275.     convolve(QUAD1,3,3,kersh1,0,POSITIVE);
  276.     printf("-- Horizontal edges in Quadrant 2 --\n");
  277.     convolve(QUAD2,5,3,kerhoriz,0,ABSOLUTE);
  278.     printf("-- Vertical edges in Quadrant 3 --\n");
  279.     convolve(QUAD3,3,5,kervert,0,ABSOLUTE);
  280.     printf("-- Laplacian of Qandrant 4 --\n");
  281.     convolve(QUAD4,3,3,kerlapla,0,ABSOLUTE);
  282.  
  283.     printf("Area operations test done.\n");
  284.     printf("Type return (ENTER) to continue:");
  285.     scanf("%c",&c);
  286.  
  287. /* Geometric operations */
  288.     printf("\nTest Geometric operations:\n");
  289.     printf("-- Take a picture --\n"); acquire();
  290.     printf("-- Draw calibration grid --\n"); draw_grid(10,15,MAXPIX);
  291.     printf("-- Rotate Quadrant 2 --\n"); rotate(QUAD2);
  292.     printf("-- Stretch Quadrant 1 by 2.0 in X --\n");
  293.     stretch(QUAD1,2.0,1.0);
  294.     printf("-- Stretch Quadrant 3 by 2.0 in Y --\n");
  295.     stretch(QUAD3,1.0,2.0);
  296.     printf("-- Stretch Quadrant 4 by 3 in X and Y (ZOOM) --\n");
  297.     stretch(QUAD4,3.0,3.0);
  298.  
  299.     printf("Geometric operations test done.\n");
  300.     printf("Type return (ENTER) to continue:");
  301.     scanf("%c",&c);
  302.  
  303. /* Measurement operations */
  304.     printf("\nTest Measurement operations:\n");
  305.     printf("-- Taking a picture --\n"); acquire();
  306.     printf("-- Histogram Quadrant 2 --\n"); histogram(QUAD2,h);
  307.     print_histogram(h);
  308.     printf("-- Plot histogram in Quadrant 1 --\n");
  309.     clear_area(QUAD1,MINPIX);
  310.     plot_histo(QUAD1,h,MAXPIX);
  311.  
  312.     printf("-- Copy Quad 3 to Quad 4 --\n");
  313.     copy_area(QUAD3,QUAD4);
  314.     printf("-- Binarize Quadrants 3 and 4 --\n");
  315.     binarize(QUAD3,PIXEL_SIZE/2);
  316.     binarize(QUAD4,PIXEL_SIZE/2);
  317.  
  318. #ifdef LUTS        /* If you have LUTS, use them to color image */
  319.     printf("-- Setting LUTs to show labeled areas --\n");
  320.     p_spectrum();
  321. /* Leave the binary colors (MINPIX and MAXPIX) alone */
  322.     write_LUT(RED,MAXPIX,MAXPIX);
  323.     write_LUT(GREEN,MAXPIX,MAXPIX);
  324.     write_LUT(BLUE,MAXPIX,MAXPIX);
  325.     write_LUT(RED,MINPIX,MINPIX);
  326.     write_LUT(GREEN,MINPIX,MINPIX);
  327.     write_LUT(BLUE,MINPIX,MINPIX);
  328. #endif
  329.     printf("Label binary objects with 20 pixels or more in Quadrant 4\n");
  330.     label(QUAD4,MINPIX,MAXPIX,20,(PIXEL)1,(PIXEL)(PIXEL_SIZE-2));
  331.     printf("Measurement operations test done.\n");
  332.  
  333.     printf("Type return (ENTER) to continue:");
  334.     scanf("%c",&c);
  335.  
  336. /* Utility operations */
  337.     printf("\Test Utility operations:\n");
  338.     printf("-- Take a picture --\n"); acquire();
  339.  
  340.     printf("-- Save Quadrant 2 --\n");
  341.     printf("File name:");
  342.     scanf("%s",name);
  343.     save_image(QUAD2,name);
  344.     printf("-- Restore Quadrant 2 image to Quadrant 4 --\n");
  345.     read_image(QUAD4,name);
  346.  
  347.     printf("Utility operations test done.\n");
  348.     printf("\n TEST DONE\n");
  349.  
  350. }
  351.  
  352. /* ======================= Internal routines ======================= */
  353.  
  354. /* WARNING:  This assumes an ASCII encoding for your characters!!! */
  355. /* Encode and match two strings, up to string length n or a null.
  356.    Returns 1 if match, 0 elsewise */
  357. static int matches(s1,s2,n)
  358. char *s1,*s2;    /* Strings to match */
  359. int n;        /* Number of characters to use */
  360. {
  361.     register int i,v;
  362.  
  363. /* If either string is NULL, return no match */
  364.     if ((*s1 == NULL) || (*s2 == NULL)) return(0);
  365.  
  366.     v = 0;
  367.     for (i = 0 ; i < n ; i++) {
  368. /* Nulls always match after one character */
  369.         if ((*s1 == NULL) || (*s2 == NULL)) return(1);
  370. /* Lower case elements.  REQUIRES ASCII ENCODING!!! */
  371.         if ((*s1 > 0100) && (*s1 <= 0132)) *s1 += 040;
  372.         if ((*s2 > 0100) && (*s2 <= 0132)) *s2 += 040;
  373.         if (*s1++ != *s2++) return(0);
  374.     }
  375.     return(1);
  376. }
  377. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  378. static int get_quad()        /* Get a quadrant number (0,1,2,3,4) */
  379. {
  380.     int q;
  381.  
  382.     for (;;) {
  383.         printf("Quadrant number = ");
  384.         scanf("%d",&q);
  385.         if ((q < 0) || (q > MAX_QUAD))
  386.           printf("Quadrant number must be 0 (for all) to %d!\n",
  387.             MAX_QUAD);
  388.         else
  389.           return(q);
  390.     }
  391. }
  392.  
  393. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  394. int get_int(s)        /* Print string and return an integer value */
  395. char *s;
  396. {
  397.     int i;
  398.  
  399.     printf("%s ",s);
  400.     scanf("%d",&i);
  401.     return(i);
  402. }
  403.  
  404.  
  405. /* ================ End of simtest.c ================ */
  406.  
  407. /* <-- FILE BREAK --> */
  408.